﻿using System;
using System.Data;
using System.Data.SqlClient;
using Koonsoft.HYCRM.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Koonsoft.HYCRM.DAL
{
    public class ClientDAL
    {
        private static readonly Database Database = DatabaseFactory.CreateDatabase("KOONCRMDB");

        /// <summary>
        /// 根据订单号获取客户信息
        /// </summary>
        /// <param name="orderID"></param>
        /// <returns></returns>
        public ClientInfo GetClientInfoByOrderID(string orderID)
        {
            const string procedure = "Client_GetDeliverInfoByOrderID";

            using (var cmd = Database.GetStoredProcCommand(procedure))
            {
                Database.AddInParameter(cmd, "@OrderID", DbType.String, orderID);

                var rd = (RefCountingDataReader)Database.ExecuteReader(cmd);

                var sqlrd = (SqlDataReader)rd.InnerReader;

                if (sqlrd.HasRows)
                {
                    sqlrd.Read();

                    return new ClientInfo
                    {
                        Name = sqlrd["ClientName"].ToString(),
                        Address = sqlrd["Address"].ToString(),
                        AgentCompanyName = sqlrd["AgentCompanyName"].ToString(),
                        Email = sqlrd["Email"].ToString(),
                        Mobile = sqlrd["Mobile"].ToString(),
                        Fax = sqlrd["Fax"].ToString(),
                        IM = sqlrd["IM"].ToString(),
                        Telephone = sqlrd["Telephone"].ToString(),
                        Type = (ClientType)Convert.ToInt32(sqlrd["Type"])
                    };
                }
            }

            return null;
        }


        /// <summary>
        /// 根据订单号获取客户信息
        /// </summary>
        /// <param name="clientID"></param>
        /// <returns></returns>
        public ClientInfo GetClientInfoByClientID(string clientID)
        {
            const string sql = "select * from client where ClientID=@clientID";

            using (var cmd = Database.GetSqlStringCommand(sql))
            {
                Database.AddInParameter(cmd, "@clientID", DbType.String, clientID);

                var rd = (RefCountingDataReader)Database.ExecuteReader(cmd);

                var sqlrd = (SqlDataReader)rd.InnerReader;

                if (sqlrd.HasRows)
                {
                    sqlrd.Read();

                    return new ClientInfo
                    {
                        Name = sqlrd["ClientName"].ToString(),
                        Address = sqlrd["Address"].ToString(),
                        AgentCompanyName = sqlrd["AgentCompanyName"].ToString(),
                        Email = sqlrd["Email"].ToString(),
                        Mobile = sqlrd["Mobile"].ToString(),
                        Fax = sqlrd["Fax"].ToString(),
                        IM = sqlrd["IM"].ToString(),
                        Telephone = sqlrd["Telephone"].ToString(),
                        Type = (ClientType)sqlrd["Type"]
                    };
                }
            }

            return null;
        }
    }
}
